package com.sq.partner.manager.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.dao.CardInfoDao;
import com.sq.partner.manager.mybatis.model.CardInfo;

/**
 * 激活卡单Dao实现
 * 
 * SQ
 *
 */
@Repository
public class CardInfoDaoImpl extends BaseDaoImpl implements CardInfoDao {

    /**
     * 取得激活卡信息
     * 
     * @param cardNo
     * @return
     * @throws Exception
     */
    @Override
    public CardInfo getCardInfoByCardNo(String cardNo) throws Exception {
        CardInfo cardInfo = (CardInfo) this
                .query("getCardInfoByCardNo", cardNo);
        return cardInfo;
    }

    /**
     * 更新卡单表的暂存单号
     * 
     * @param cardNo
     * @param stagNo
     * @throws Exception
     */
    @Override
    public Result updateStagNoByCardNo(Map<String, String> param)
            throws Exception {
        boolean flag = this.update("updateStagNoByCardNo", param);
        if (!flag) {
            return Result.error("卡单表中暂存单号置空失败，不可以重新投保！");
        }
        return Result.success("卡单表中暂存单号置空成功，可以重新投保！");
    }

    @Override
    public void updateCardStatus(CardInfo cardInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("stagNo", cardInfo.getStagNo());
        map.put("cardNo", cardInfo.getCardNo());
        map.put("statusBef", Constants.ACTIR_CARD_ACTIVABLE);
        map.put("statusAft", Constants.ACTIR_CARD_ACTIVABLE_USED);
        map.put("appDate", cardInfo.getAppDate());
        map.put("effDate", cardInfo.getEffDate());
        this.update("updateStatusByCardNo", map);
    }
    
    /**
     * 根据暂存单号和投保单号取得卡单信息
     * @param appNo
     * @param stagNo
     * @return
     * @throws Exception
     */
    @Override
    public CardInfo getCardInfoByAppNo(String appNo, String stagNo) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        map.put("appNo", appNo);
        map.put("stagNo", stagNo);
        
        return (CardInfo) this.query("getCardInfoByAppNo", map);
    }
    
    /**
     * 根据暂存单号和投保单号取得卡号
     * @param appNo
     * @param stagNo
     * @return
     * @throws Exception
     */
    @Override
    public String getCardNoByAppNo(String appNo, String stagNo) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        map.put("appNo", appNo);
        map.put("stagNo", stagNo);
        
        return (String) this.query("getCardNoByAppNo", map);
    }
    
    /**
     * 查询暂存单下的卡总张数
     * @param stagNo
     * @return
     * @throws Exception
     */
    @Override
    public int queryCardCountByStagNo(String stagNo) throws Exception {
        return (Integer) this.query("queryCardCountByStagNo", stagNo);
    }
    
    /**
     * 把暂存单号下的所有卡单的暂存单号和投保单号置空
     * @param map
     * @return
     * @throws Exception
     */
    @Override
    public boolean updateStagNoAndAppNoByStagNo(Map<String, String> map) throws Exception {
        return this.update("updateStagNoAndAppNoByStagNo", map);
    }
    
    /**
     * 出库ID取得卡单信息
     * @param commamtId
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<CardInfo> getCardInfoListByCommantId(String commamtId) throws Exception {
        return (List<CardInfo>) this.queryList("getCardInfoListByCommantId",
                commamtId);
    }

    // 集中收款平台对接 2019/03/12 zhouxx start
    /**
     * 取同一個出库Id下的所有卡中最小的失效日期
     * @param commamtId
     * @return
     * @throws Exception
     */
    @Override
    public Date getMinDisableDate(String commamtId) throws Exception {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("commamtId", commamtId);
        return (Date) this.query("queryMinDisableDateByCommamtId", map);
    }
    // 集中收款平台对接 2019/03/12 zhouxx end
    
    /**
     * 根据暂存单号和投保单号取得卡号 是否有价状态   20190329 helina
     * @param appNo
     * @param stagNo
     * @return
     * @throws Exception
     */
    @Override
    public CardInfo getCardNoPriceByAppNo(String appNo, String stagNo) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        map.put("appNo", appNo);
        map.put("stagNo", stagNo);
        
        return (CardInfo) this.query("getCardNoPriceByAppNo", map);
    }
}
